package com.yunhe.authority.service.company.impl;

import com.alibaba.fastjson.JSON;
import com.yunhe.authority.domain.company.*;
import com.yunhe.authority.model.paymentInfo.IncreasePercentVO;
import com.yunhe.authority.repository.company.CompanyCrudRepository;
import com.yunhe.authority.repository.paymentInfo.*;
import com.yunhe.authority.service.company.PaymentInfoService;
import com.yunhe.common.exception.ArgumentErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @ClassName: PaymentInfoServiceImpl
 * @Description:
 * @author: GongDaCheng
 * @Date: 15:58 2021/7/19
 */
@Service
public class PaymentInfoServiceImpl implements PaymentInfoService {

    @Autowired
    PropertyCostRepository propertyCostRepository;

    @Autowired
    IncreasePercentRepository increasePercentRepository;

    @Autowired
    WaterElectricityCostRepository waterElectricityCostRepository;

    @Autowired
    OtherCostRepository otherCostRepository;

    @Autowired
    ContractAccessoriesRepository contractAccessoriesRepository;

    @Autowired
    CompanyCrudRepository companyCrudRepository;

    @Override
    public PropertyCost savePropertyCost(Long companyId, PropertyCost propertyCost) throws ArgumentErrorException {
        Company company = companyCrudRepository.findById(companyId).orElse(null);
        if(company == null){
            throw new ArgumentErrorException("选择的企业信息不存在,请重新选择相关企业");
        }
        company.setPaymentDate(propertyCost.getPaymentDate());
        companyCrudRepository.save(company);
        PropertyCost save = propertyCostRepository.save(propertyCost);
        PropertyCost getByCompanyId = propertyCostRepository.getByCompanyId(companyId);
        if (getByCompanyId == null){
            //查询为空，即新增
            Long id = save.getId();
            propertyCostRepository.bindCompany(companyId, id);
        }
        return save;
    }

    @Override
    public PropertyCost getPropertyCostByCompanyId(Long companyId) throws ArgumentErrorException {
        PropertyCost result = new PropertyCost();
        Company company = companyCrudRepository.findById(companyId).orElse(null);
        if(company == null){
            throw new ArgumentErrorException("选择的企业信息不存在,请重新选择相关企业");
        }
        PropertyCost getByCompanyId = propertyCostRepository.getByCompanyId(companyId);
        if (getByCompanyId != null){
            result = getByCompanyId;
        }
        result.setPaymentDate(company.getPaymentDate());
        return result;
    }

    @Override
    public IncreasePercent saveIncreasePercent(Long companyId, IncreasePercentVO increasePercentVO) {
        IncreasePercent increasePercent = new IncreasePercent();
        Map map = increasePercentVO.getIncreasePercent();
        String increasePercentStr = JSON.toJSONString(map);

        increasePercent.setId(increasePercentVO.getId());
        increasePercent.setIncreasePercent(increasePercentStr);
        IncreasePercent save = increasePercentRepository.save(increasePercent);
        IncreasePercent getByCompanyId = increasePercentRepository.getByCompanyId(companyId);
        if (getByCompanyId == null){
            Long id = increasePercent.getId();
            increasePercentRepository.bindCompany(companyId, id);
        }
        return save;
    }

    @Override
    public IncreasePercentVO getIncreasePercentByCompanyId(Long companyId) {
        IncreasePercent getByCompanyIdStr = increasePercentRepository.getByCompanyId(companyId);
        if (getByCompanyIdStr == null){
            return new IncreasePercentVO();
        }
        Map increasePercent = JSON.parseObject(getByCompanyIdStr.getIncreasePercent());
        IncreasePercentVO increasePercentVO = new IncreasePercentVO();
        increasePercentVO.setId(getByCompanyIdStr.getId());
        increasePercentVO.setIncreasePercent(increasePercent);
        return increasePercentVO;
    }

    @Override
    public WaterElectricityCost saveWEC(Long companyId, WaterElectricityCost waterElectricityCost) {
        WaterElectricityCost save = waterElectricityCostRepository.save(waterElectricityCost);
        WaterElectricityCost getByCompanyId = waterElectricityCostRepository.getByCompanyId(companyId);
        if (getByCompanyId == null){
            //该企业不存在水电费缴费信息，保存并绑定
            Long wecId = waterElectricityCost.getId();
            waterElectricityCostRepository.bindCompany(companyId, wecId);
        }
        return save;
    }

    @Override
    public WaterElectricityCost getWECByCompanyId(Long companyId) throws ArgumentErrorException {
        Company company = companyCrudRepository.findById(companyId).orElse(null);
        if(company == null){
            throw new ArgumentErrorException("选择的企业信息不存在,请重新选择相关企业");
        }
        WaterElectricityCost getByCompanyId = waterElectricityCostRepository.getByCompanyId(companyId);
        if (getByCompanyId == null){
            return new WaterElectricityCost();
        }
        return getByCompanyId;
    }

    @Override
    public OtherCost saveOtherCost(Long companyId, OtherCost otherCost) {
        OtherCost save = otherCostRepository.save(otherCost);
        OtherCost getByCompanyId = otherCostRepository.getByCompanyId(companyId);
        if (getByCompanyId == null){
            Long ocId = otherCost.getId();
            otherCostRepository.bindCompany(companyId, ocId);
        }
        return save;
    }

    @Override
    public OtherCost getOtherCostByCompanyId(Long companyId) throws ArgumentErrorException {
        Company company = companyCrudRepository.findById(companyId).orElse(null);
        if(company == null){
            throw new ArgumentErrorException("选择的企业信息不存在,请重新选择相关企业");
        }
        OtherCost getByCompanyId = otherCostRepository.getByCompanyId(companyId);
        if (getByCompanyId == null){
            return new OtherCost();
        }
        return getByCompanyId;
    }

    @Override
    public ContractAccessories createContract(Long companyId, ContractAccessories contractAccessories) {
        ContractAccessories save = contractAccessoriesRepository.save(contractAccessories);
        Long id = contractAccessories.getId();
        contractAccessoriesRepository.bindCompany(companyId, id);
        return save;
    }

    @Override
    public List<ContractAccessories> findAllContract(Long companyId) {
        List<ContractAccessories> all = contractAccessoriesRepository.findAll(companyId);
        return all;
    }

    @Override
    public ContractAccessories updateContract(Long companyId, Long id, ContractAccessories contractAccessories) {
        //先解绑删除
        contractAccessoriesRepository.unbindAndDelete(id);
        //再新建绑定
        ContractAccessories save = contractAccessoriesRepository.save(contractAccessories);
        contractAccessoriesRepository.bindCompany(companyId, save.getId());
        return save;
    }

    @Override
    public void deleteContract(Long id) {
        contractAccessoriesRepository.unbindAndDelete(id);
    }

    @Override
    public void unbindDeleteCompanyPaymentInfo(Long companyId) {
        //解绑并删除场地/物业费用
        propertyCostRepository.unbindAndDelete(companyId);
        //解绑并删除增幅比例
        increasePercentRepository.unbindAndDelete(companyId);
        //解绑并删除水电费费用
        waterElectricityCostRepository.unbindAndDelete(companyId);
        //解绑并删除其他费用
        otherCostRepository.unbindAndDelete(companyId);
        //解绑并删除合同附件
        contractAccessoriesRepository.unbindAndDelete(companyId);
    }


}
